package com.example.mail;


import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TabHost;
import com.example.MessageCodes;
import com.example.R;
import core.model.Game;
import core.model.mail.Context;
import core.util.Logger;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MailTabActivity extends TabActivity {

    private Context context = new Context();
    private List<String> races = new ArrayList<String>();
    private MailManager mailManager;
    private Game game;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mailtabs);

        game = (Game) this.getIntent().getExtras().getSerializable(Game.GAME);

        mailManager = new MailManager(this, game, handler);
        mailManager.loadMessages();
    }

    private void buildUI() {
        context = mailManager.getContext();
        races.addAll(mailManager.getRaces());

        Resources res = getResources();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

        // intent for news
        intent = new Intent().setClass(this, MailNewsActivity.class);

        // spec for news
        spec = tabHost.newTabSpec("News").setIndicator("News",
                res.getDrawable(R.drawable.ic_tab_news))
                .setContent(intent);
        tabHost.addTab(spec);

        // intent for broadcasts
        intent = new Intent().setClass(this, MailListActivity.class);
        intent.putExtra(Game.GAME, game);
        intent.putExtra("selectedMailType", "Broadcasts");
        intent.putExtra("messages", (Serializable) Collections.synchronizedList(context.getBroadcasts()));
        intent.putExtra("races", (Serializable) Collections.synchronizedList(races));

        // spec for broadcasts
        spec = tabHost.newTabSpec("broadcasts").setIndicator("Broadcasts",
                res.getDrawable(R.drawable.ic_tab_broadcasts))
                .setContent(intent);
        tabHost.addTab(spec);

        // intent for privates
        intent = new Intent().setClass(this, MailListActivity.class);
        intent.putExtra(Game.GAME, game);
        intent.putExtra("selectedMailType", "Privates");
        intent.putExtra("messages", (Serializable) Collections.synchronizedList(context.getPrivates()));
        intent.putExtra("races", (Serializable) Collections.synchronizedList(races));

        // spec for privates
        spec = tabHost.newTabSpec("Privates").setIndicator("Privates",
                res.getDrawable(R.drawable.ic_tab_privates))
                .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
    }

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case MessageCodes.Mail.LOADING_RACES_COMPLETE:
                    Logger.debug("MailTabActivity", "Races loading was complete without any problems.");
                    buildUI();
                    break;
                case MessageCodes.Mail.LOADING_MESSAGES_COMPLETE:
                    Logger.debug("MailTabActivity", "Mail loading was complete without any problems.");
                    mailManager.loadRaces();
                    break;
                case MessageCodes.EXCEPTION:
                    Logger.debug("MailTabActivity", "Mail loading was not complete.");
                    break;
            }

        }
    };
}
